package com.xhs.zdl.demo.treeNode;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.xhs.zdl.demo.R;
import com.xhs.zdl.demo.tools.MyNormalUtils;
import com.xhs.zdl.demo.tools.MyPhoneUtils;

import java.util.List;

/**
 * @author zdl
 * @date 2018/2/6 14:54
 * email zdl328465042@163.com
 * explain
 */

public class TreeNodeAdapter extends BaseAdapter {
    private Context context;
    private List<TreeNodeBean> data;
    private LayoutInflater inflater;
    private AddOrMinusClickListener clickListener;

    public TreeNodeAdapter(Context context, List<TreeNodeBean> data) {
        this.context = context;
        this.data = data;
        inflater = LayoutInflater.from(context);
    }

    public void setClickListener(AddOrMinusClickListener clickListener) {
        this.clickListener = clickListener;
    }

    @Override
    public int getCount() {
        return data.size();
    }

    @Override
    public Object getItem(int i) {
        return data.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(final int i, View view, ViewGroup viewGroup) {
        ViewHolder holder;
        if (null == view){
            holder = new ViewHolder();
            view = inflater.inflate(R.layout.item_listview_treenode, viewGroup, false);
            holder.treeNodeInfo = view.findViewById(R.id.treeNodeInfo);
            holder.treeNodeMinus = view.findViewById(R.id.treeNodeMinus);
            holder.treeNodeAdd = view.findViewById(R.id.treeNodeAdd);

            view.setTag(holder);
        }else {
            holder = (ViewHolder) view.getTag();
        }

        TreeNodeBean treeNodeBean = data.get(i);
        final int level = treeNodeBean.getLevel();
        view.setPadding(MyPhoneUtils.dip2px(context, 30 * (level + 2)), MyPhoneUtils.dip2px(context, 10), MyPhoneUtils.dip2px(context, 10), MyPhoneUtils.dip2px(context, 10));

        holder.treeNodeInfo.setText(treeNodeBean.getInfo());
        if (-1 == treeNodeBean.getLevel()){
            holder.treeNodeMinus.setVisibility(View.GONE);
        }else {
            holder.treeNodeMinus.setVisibility(View.VISIBLE);
        }
        holder.treeNodeAdd.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (null != clickListener){
                    clickListener.addNode(level+1, i);
                }
            }
        });
        holder.treeNodeMinus.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (null != clickListener){
                    clickListener.minusNode(level, i);
                }
            }
        });

        return view;
    }

    class ViewHolder{
        TextView treeNodeInfo;
        ImageView treeNodeMinus, treeNodeAdd;
    }
}
